﻿using System.Collections.Generic;
using System.IO;
using ExcelAutomator.Configuration;
using ExcelAutomator.Validation;
using NUnit.Framework;

namespace ExcelAutomator.Tests.IntegrationTests.RangeExecutors
{
	[TestFixture]
	public class RangeCopyTestFixture : ExcelTestFixtureBase
	{

		#region FailValidationIfSourceAddressIsNotGiven
		[Test]
		public void FailValidationIfSourceAddressIsNotGiven()
		{
			var cfg = new ExcelAutomatorConfiguration()
			          	{
			          		Settings = new SettingsConfiguration(){Input = SourceWbName}, 
										ExecutionSets = new List<ExecutionSetConfiguration>()
										                	{
										                		new ExecutionSetConfiguration(){
																					Id = "My Id", 
																					Executors = new List<ExecutorConfigurationWrapper>()
																					            	{
																					            		new ExecutorConfigurationWrapper("range", 
																														Constants.ExecutorExtensionsNamespace, 
																														string.Format(@"<range action='copy' sourceAddress='' targetAddress='Sheet1!A2' xmlns='{0}'/>", Constants.ExecutorExtensionsNamespace))
																					            	}}
										                	}
			          	};

			var sut = new ExcelAutomationService(cfg);

			ValidationReport report;
			sut.Validate(out report);

			Assert.That(report.IsValid, Is.False);
			Assert.That(report.ViolationsCount, Is.EqualTo(1));
		}
		#endregion

		#region FailValidationIfTargetAddressIsNotGiven
		[Test]
		public void FailValidationIfTargetAddressIsNotGiven()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = SourceWbName
				},
				ExecutionSets = new List<ExecutionSetConfiguration>()
										                	{
										                		new ExecutionSetConfiguration(){
																					Id = "My Id", 
																					Executors = new List<ExecutorConfigurationWrapper>()
																					            	{
																					            		new ExecutorConfigurationWrapper("range", 
																														Constants.ExecutorExtensionsNamespace, 
																														string.Format(@"<range action='copy' sourceAddress='Sheet1!A2' targetAddress='' xmlns='{0}'/>", Constants.ExecutorExtensionsNamespace))
																					            	}}
										                	}
			};

			var sut = new ExcelAutomationService(cfg);

			ValidationReport report;
			sut.Validate(out report);

			Assert.That(report.IsValid, Is.False);
			Assert.That(report.ViolationsCount, Is.EqualTo(1));
		}
		#endregion

		#region CopyPlainTextValueFromSourceToTarget
		[Test]
		public void CopyPlainTextValueFromSourceToTarget()
		{
			var cfg = ExcelAutomatorConfiguration.FromXml(TestHelper.GetStringResource("ExcelAutomator.Tests.Resources.CopyRangeToTarget.xml"));
		  var sut = new ExcelAutomationService(cfg);

		  sut.ExecuteAutomation();

			base.StartExcel();
		  ResolveRangeValues(Path.Combine(TargetFolderPath, TargetWbName), "Sheet1", "B1", x1 => Assert.That(x1, Is.EqualTo("TestData1")));
		  ResolveRangeValues(Path.Combine(TargetFolderPath, TargetWbName), "Sheet1", "B2", x1 => Assert.That(x1, Is.EqualTo("TestData2")));
		  ResolveRangeValues(Path.Combine(TargetFolderPath, TargetWbName), "Sheet1", "B3", x1 => Assert.That(x1, Is.EqualTo("TestData3")));
		  ResolveRangeValues(Path.Combine(TargetFolderPath, TargetWbName), "Sheet1", "B4", x1 => Assert.That(x1, Is.EqualTo("TestData4")));
		  ResolveRangeValues(Path.Combine(TargetFolderPath, TargetWbName), "Sheet1", "B5", x1 => Assert.That(x1, Is.EqualTo("TestData5")));
		  base.QuitExcel();
		}
		#endregion

		#region CopyRangeWithinInputFile
		[Test]
		public void CopyRangeWithinInputFile()
		{
			var cfg =
				ExcelAutomatorConfiguration.FromXml(
					TestHelper.GetStringResource("ExcelAutomator.Tests.Resources.CopyRangeWithinInputFile.xml"));
		  var sut = new ExcelAutomationService(cfg);

		  sut.ExecuteAutomation();

			base.StartExcel();
		  ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "A1", x1 => Assert.That(x1, Is.EqualTo("TestData1")));
		  ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "A2", x1 => Assert.That(x1, Is.EqualTo("TestData2")));
		  ResolveRangeValues(Path.Combine(InputFolderPath, SourceWbName), "Sheet2", "A3", x1 => Assert.That(x1, Is.EqualTo("TestData3")));
		  base.QuitExcel();
		}
		#endregion

		#region CopyToTarget_SaveAsNewFilename
		[Test]
		public void CopyToTarget_SaveAsNewFilename()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = @"Input\" + SourceWbName,
					Target = @"Target\" + TargetWbName,
					OutputFolder = @"Output",
					FilenamePattern = "%inputFilename_copy.xlsx"
				},
				ExecutionSets = new List<ExecutionSetConfiguration>()
				                	{
				                		new ExecutionSetConfiguration()
				                			{
				                				Id = "My Execution Set", 
																Executors = new List<ExecutorConfigurationWrapper>()
																            	{
																            		new ExecutorConfigurationWrapper(
																									"range", 
																									Constants.ExecutorExtensionsNamespace, 
																									string.Format(@"<range action='copy' sourceAddress='Sheet1!A1' targetAddress='Sheet1!A1' xmlns='{0}'/>", Constants.ExecutorExtensionsNamespace))
																            	}
				                			}
				                	}
			};
		  var sut = new ExcelAutomationService(cfg);

		  sut.ExecuteAutomation();

		  var newFilename1 = string.Format("Book_source_copy.xlsx");

		  Assert.That(File.Exists(Path.Combine(OutputFolderPath, newFilename1)));

			base.StartExcel();
			ResolveRangeValues(Path.Combine(OutputFolderPath, newFilename1), "Sheet1", "A1", a1 => Assert.That(a1, Is.EqualTo("TestData1")));
		  base.QuitExcel();
		}
		#endregion


		#region SaveInSpecifiedOutputDirectory
		[Test]
		public void SaveInSpecifiedOutputDirectory()
		{
			var cfg = new ExcelAutomatorConfiguration()
			{
				Settings = new SettingsConfiguration()
				{
					Input = @"Input\" + SourceWbName,
					OutputFolder = @"Output"
				},
				ExecutionSets = new List<ExecutionSetConfiguration>()
				                	{
				                		new ExecutionSetConfiguration()
				                			{
				                				Id = "My Execution Set", 
																Executors = new List<ExecutorConfigurationWrapper>()
																            	{
																            		new ExecutorConfigurationWrapper(
																									"range", 
																									Constants.ExecutorExtensionsNamespace, 
																									string.Format(@"<range action='copy' sourceAddress='Sheet1!A1' targetAddress='Sheet2!A1' xmlns='{0}'/>", Constants.ExecutorExtensionsNamespace))
																            	}
				                			}
				                	}
			};

			var sut = new ExcelAutomationService(cfg);

			sut.ExecuteAutomation();

			Assert.That(File.Exists(Path.Combine(OutputFolderPath, SourceWbName)));
		}
		#endregion
		
		
		

	}
}
